
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="/static/crm/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/crm/css/public.css" media="all">
    <link rel="stylesheet" href="/static/crm/lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
</head>

<body>
    <div class="layuimini-container">
        <form id="app-form" class="layuimini-form ">
            <div class="layui-form-item">
                <label class="layui-form-label required">权限名称</label>
                <div class="layui-input-block">
                    <input type="text" name="title" readonly class="layui-input" value="{$role.auth_name}">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">分配节点</label>
                <div class="layui-input-block">
                    <div id="auth_node" class="demo-tree-more"></div>
                </div>
            </div>

            <div class="layui-form-item text-center" style="padding-left:45%;">
                <button type="submit" class="layui-btn layui-btn-normal layui-btn-sm" lay-submit
                    lay-filter="node-add">确认</button>
                <button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">重置</button>
            </div>

        </form>
    </div>
    <script src="/static/crm/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
    <script>
        layui.use(['tree', 'jquery', 'form'], function () {
            var tree = layui.tree;
            var $ = layui.jquery;
            var form = layui.form;

            //树形组件
            $.ajax({
                url: '/crm/auth.role/authorize',
                dataType: 'json',
                success: function (res) {
                    tree.render({
                        elem: '#auth_node',
                        showCheckbox: true,
                        data: res,
                        id: 'treeId'
                    });
                }
            })



            form.on('submit(node-add)', function (data) {
                //获得选中的节点
                var treecheckdata = tree.getChecked('treeId');
                var nodeIds = new Array();
                nodeIds = getCheckedId(treecheckdata);
                $.ajax({
                    type: 'post',
                    url: "/crm/auth.role/authorize/id/{$role.id}",
                    dataType: 'json',
                    data: {nodeIds : nodeIds},
                    success: function (result) {
                        if (result.code == 0) {
                            layer.msg(result.msg, {
                                time: 800,
                                end: function () {
                                    var iframeIndex = parent.layer.getFrameIndex(window.name);
                                    parent.layer.close(iframeIndex);
                                }
                            });
                        } else {
                            layer.msg(result.msg);
                        }
                    }
                })
                return false;
            });

            // 获取选中节点的id
            function getCheckedId(jsonObj) {
                var id = "";
                $.each(jsonObj, function (index, item) {
                    if (id != "") {
                        id = id + "," + item.id;
                    }
                    else {
                        id = item.id;
                    }
                    var i = getCheckedId(item.children);
                    if (i != "") {
                        id = id + "," + i;
                    }
                });
                return id;
            }
        });
    </script>
</body>

</html>